<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class scache extends CI_Model {

    function __construct() {
        parent::__construct();
    }
	
	function html(){
		 if($this->config->item('CACHE_ON') == 1){
			$controler = $this->router->fetch_class();
			$class = $this->router->fetch_method();
			$cache = $this->config->item('cachePosition');
			$position = strtoupper($controler."_".$class);
			$cacheTime = $this->config->item('CACHE_'.$position);
			if(isset($cacheTime) && $cacheTime && $cacheTime > 0){
				$this->output->cache($cacheTime);
			}
			/*
			if(array_key_exists($position,$cache)){
				$n = $cache[$position];
				$this->output->cache($n);
			}
			*/
		 }
	}
	
	function script($cache_name, $PHP_RunScript, $time_cache = 0){
		if($time_cache ==0)
			$time_cache = $this->config->item('CACHE_FUNCTION');//Seconds
		if($time_cache <=0){
			return eval("return ".$PHP_RunScript);
		}
		$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
		$cache_name = $cache_name.".cache";
		if ( !$Data = $this->cache->get($cache_name))
		{
			$time_cache = $time_cache*60;
			$Data= eval("return ".$PHP_RunScript);
			$this->cache->save($cache_name, $Data, $time_cache);
		}
		return $Data;
	}
	
	function save_data($cache_name, $data){
		$cache_name = "data_".$cache_name.".cache";
		$time_cache = $this->config->item('CACHE_FUNCTION');//Seconds
		if($time_cache <=0){
			return 0;
		}
		$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
		$time_cache = $time_cache*60;
		$this->cache->save($cache_name, $data, $time_cache);
		return 1;
	}
	
	function load_data($cache_name){
		$cache_name = "data_".$cache_name.".cache";
		$time_cache = $this->config->item('CACHE_FUNCTION');//Seconds
		if($time_cache <=0){
			return null;
		}
		$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
		if ( !$mdata = $this->cache->get($cache_name))
		{
			return null;
		}
		return $mdata;
	}
	
	function delete_data($cache_name){
		$this->delete("data_".$cache_name.".cache");
	}
	
	function delete($cache_name){
		$this->cache->delete($cache_name.".cache");
	}
}

?>